/**
* The LightServer is the server that the game runs on. It listens for conections from clients. Creates ServerThreadHandlers to handle
* the sending of packets back and forth to clients/server
* Group: Y1
* @author Mason Kwiat
*/


//package network;
package lightspeed.Network;

import lightspeed.Backend.GameManager;
import java.net.*;
import java.io.*;

import lightspeed.Media.Graphics.WindowManager;


public class LightServer
{
	/**
	*	integer variable to describe the number of client's connected to the game.
	*/
	public static int numCon = 0;
	/**
	*	integer variable to describe maxumim number of client's that can connect to the game (10).
	*/
	public static int maxCon = 10;
	/**
	*	Creates array of ServerThreadHandler with ten elements
	*/
	public static ServerThreadHandler[] threads = new ServerThreadHandler[maxCon];
	/**
	*	Creates array of ClientPacket with ten elements
	*/
	public static ClientPacket[] cpackets = new ClientPacket[maxCon];
	/**
	*	NULL ServerPacket object
	*/
	public static ServerPacket spacket = null;
	/**
	*	GameManager
	*/
	public static GameManager gameManager;
	
	public static WindowManager windowManager;
	
	
	/*//testing only
	public static void main(String[] args)
	{
		try{
		LightServer ls = new LightServer();
		}
		catch(Exception e) {}
	}
	//testing only*/
	
	/**
	*	Constructor: creates an instance of LightServer
	*	@exception throws input/output exception if fails to connect to port 5195
	*/
	public LightServer(WindowManager wm) throws IOException
	{
		windowManager = wm;
		
		ServerSocket serverSocket = null;
		spacket = new ServerPacket(maxCon);
        boolean listening = true;
        
        gameManager = new GameManager(this);
		gameManager.start();
        
        // Attempt to create socket for server
        try
        {
            serverSocket = new ServerSocket(5195);
        }
        catch (IOException e) 
        {
            System.err.println("Could not listen on port: 5195.");
            System.exit(-1);
        }
        //
        
        // print ip for testing purposes
        System.out.println("Local IP Address: " + java.net.InetAddress.getLocalHost().getHostAddress());
        //System.out.println("Global IP Address: " + globalIP());
        //
        
        //wait for connections
        while (listening)
        {
        	if (numCon <= maxCon)
        	{
	    		new ServerThreadHandler(serverSocket.accept(), numCon);
	    		numCon++;
	    		System.out.println("Connected: " + numCon);
	    	}
		}
		//
		
        serverSocket.close();
	}
	/**
	*	
	*	Method that gets global IP from internets if available
	*	@return String that holds the IP obtained from the internet
	*/
	
	// Throws exception???
	public static String globalIP()
	{
		try
		{
         	URL autoIP = new URL("http://www.whatismyip.com/automation/n09230945.asp");
         	BufferedReader in = new BufferedReader( new InputStreamReader(autoIP.openStream()));
         	String ip_address = (in.readLine()).trim();
         	in.close();
			
        	return ip_address;
        }
        catch (Exception e)
        {
	    	e.printStackTrace();
			return "-could not retrieve global ip-";
	    }
	}
	
	public void resetGame()
	{
		GameManager oldManager = gameManager;
		gameManager = new GameManager(this);
		gameManager.start();
		oldManager.stop();
	}
}
